#include "EditorAudioPageView.h"
#include "EditorAudioPageViewModel.h"

#include "Widgets/RectangleWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/CanvasWidget.h"
#include "Widgets/ImageWidget.h"

void REditorAudioPageView::BindViewModel(TSharedObjectPtr<REditorPageViewModel> InViewModel)
{
    ViewModel = InViewModel;
}

void REditorAudioPageView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    auto Root=NewWidget<RWidget>(InParent);
    Root->Anchor.Fill(InParent);

    const float Width=300;

    auto Background = NewWidget<RRectangleWidget>(Root);
    Background->Anchor.CenterIn(Root);
    Background->Width=Width;
    const float Height=60;
    Background->Height=Height;
    Background->Radius=Height*0.5f;
    Background->FillColor=SColor::White;

    auto PlayButtonBackground = NewWidget<RCanvasWidget>(Background);
    const float PlayButtonSize=Height * 0.5f;
    PlayButtonBackground->Width=PlayButtonSize;
    PlayButtonBackground->Height=PlayButtonSize;
    PlayButtonBackground->X=(Height-PlayButtonSize)*0.5f;
    PlayButtonBackground->Y=(Height-PlayButtonSize)*0.5f;

    PlayButtonBackground->AddCircle(
        SVec2(PlayButtonSize*0.5f,PlayButtonSize*0.5f)
        ,PlayButtonSize*0.5f
        ,SColor(0.8f,0.8f,0.8f,1.0f));

    const float PlayButtonIconSize=20;
    auto PlayButton = NewWidget<RImageWidget>(PlayButtonBackground);
    PlayButton->Width=PlayButtonIconSize;
    PlayButton->Height=PlayButtonIconSize;
    PlayButton->Anchor.CenterIn(PlayButtonBackground);
    PlayButton->Image=[this]()->TSharedObjectPtr<RTexture2D>
    {
        return ViewModel->GetPlayIcon();
    };

    auto PlayButtonMouse = NewWidget<RMouseWidget>(PlayButtonBackground);
    PlayButtonMouse->Anchor.Fill(PlayButtonBackground);
    PlayButtonMouse->OnClicked.BindLambda([this](const SMouseButtonEvent&)->bool
    {
        ViewModel->TogglePlay();
        return true;
    });

    const float Padding=5;
    auto TimeText = NewWidget<RTextWidget>(Background);
    TimeText->Anchor.LeftReference=PlayButtonBackground;
    TimeText->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    TimeText->Anchor.LeftOffset=Padding;
    TimeText->Width=80;
    TimeText->Height=30;
    TimeText->Anchor.VerticalCenterReference=Background;
    TimeText->Text=[this]()->CString
    {
        return ViewModel->GetTimeText();
    };

    TimeText->VerticalAlignment=ETextVerticalAlignment::Center;
    TimeText->Color=SColor::Black;

    // auto Test=NewWidget<RRectangleWidget>(Background);
    // Test->Anchor.Fill(TimeText);
    // Test->FillColor=SColor::Green;

    const float ProgressBarHeight=10;
    auto ProgressBarBackground = NewWidget<RRectangleWidget>(Background);
    ProgressBarBackground->Anchor.LeftReference=TimeText;
    ProgressBarBackground->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    ProgressBarBackground->Anchor.LeftOffset=Padding;
    ProgressBarBackground->Anchor.RightReference=Background;
    ProgressBarBackground->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Right;
    ProgressBarBackground->Anchor.RightOffset= Height*0.5f;

    ProgressBarBackground->Height=ProgressBarHeight;
    ProgressBarBackground->Anchor.VerticalCenterReference=Background;
    ProgressBarBackground->FillColor=SColor(0.5f,0.5f,0.5f,1.0f);
    ProgressBarBackground->Radius=ProgressBarHeight*0.5f;
    auto ProgressBar = NewWidget<RRectangleWidget>(Background);
    ProgressBar->Anchor.LeftReference=ProgressBarBackground;
    ProgressBar->Anchor.TopReference=ProgressBarBackground;
    ProgressBar->Height=ProgressBarHeight;
    RRectangleWidgetWeakPtr PlayTimeProgressBarBackgroundWeak=ProgressBarBackground;
    ProgressBar->Width=[this,PlayTimeProgressBarBackgroundWeak]()->float
    {
        return ViewModel->GetCurrentTimePercent()*PlayTimeProgressBarBackgroundWeak->PresentSize.X;
    };
    ProgressBar->FillColor=SColor::Black;
    ProgressBar->Radius=ProgressBarHeight*0.5f;

    const float DragBallSize=20;


    auto Ball = NewWidget<RRectangleWidget>(ProgressBar);
    Ball->Width=DragBallSize;
    Ball->Height=DragBallSize;
    Ball->Anchor.HorizontalCenterReference=ProgressBar;
    Ball->Anchor.HorizontalCenterReferenceEdge=EHorizontalWidgetEdge::Right;
    Ball->Anchor.VerticalCenterReference=Background;
    Ball->Radius=DragBallSize*0.5f;
    Ball->FillColor=SColor::Black;

    auto ProgressDragBall = NewWidget<RMouseWidget>(ProgressBarBackground);
    ProgressDragBall->Height=DragBallSize;
    ProgressDragBall->Anchor.VerticalCenterReference=ProgressBarBackground;
    ProgressDragBall->Anchor.HorizontalFill(ProgressBarBackground);
    ProgressDragBall->Drag->bIsEnabled=true;
    ProgressDragBall->Drag->OnDragMoved=[this,PlayTimeProgressBarBackgroundWeak](
        const SVec2& )
    {
        float NewX=PlayTimeProgressBarBackgroundWeak->GetMousePositionInWidget().X;
        float NewPercent=CMath::Clamp(NewX/PlayTimeProgressBarBackgroundWeak->PresentSize.X,0.0f,1.0f);
        ViewModel->SetCurrentTimePercent(NewPercent);
    };



    auto VolumeBar= NewWidget<RRectangleWidget>(Root);
    VolumeBar->Width= 300;
    VolumeBar->Height=Height;
    VolumeBar->Anchor.HorizontalFill(Background);
    VolumeBar->Anchor.TopReference=Background;
    VolumeBar->Anchor.TopReferenceEdge=EVerticalWidgetEdge::Bottom;
    VolumeBar->Anchor.TopOffset=30;
    VolumeBar->FillColor=SColor::White;
    VolumeBar->Radius=Height*0.5f;



    auto VolumeButton= NewWidget<RRectangleWidget>(VolumeBar);
    VolumeButton->Width=PlayButtonSize;
    VolumeButton->Height=PlayButtonSize;
    VolumeButton->X=(Height-PlayButtonSize)*0.5f;
    VolumeButton->Y=(Height-PlayButtonSize)*0.5f;
    VolumeButton->Radius=PlayButtonSize*0.5f;
    VolumeButton->FillColor=SColor(0.8f,0.8f,0.8f,1.0f);

    auto VolumeIcon = NewWidget<RImageWidget>(VolumeButton);
    VolumeIcon->Width= PlayButtonIconSize;
    VolumeIcon->Height=PlayButtonIconSize;
    VolumeIcon->Anchor.CenterIn(VolumeButton);

    VolumeIcon->Image=[this]()->TSharedObjectPtr<RTexture2D>
    {
        return ViewModel->GetVolumeIcon();
    };

    auto VolumeButtonMouse = NewWidget<RMouseWidget>(VolumeButton);
    VolumeButtonMouse->Anchor.Fill(VolumeButton);
    VolumeButtonMouse->OnClicked.BindLambda([this](const SMouseButtonEvent&)->bool
    {
        ViewModel->ToggleMute();
        return true;
    });

    auto VolumeBarBackground = NewWidget<RRectangleWidget>(VolumeBar);
    VolumeBarBackground->Height=ProgressBarHeight;
    VolumeBarBackground->Anchor.LeftReference=VolumeButton;
    VolumeBarBackground->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
    VolumeBarBackground->Anchor.LeftOffset=Padding;

    VolumeBarBackground->Anchor.RightReference=VolumeBar;
    VolumeBarBackground->Anchor.RightReferenceEdge=EHorizontalWidgetEdge::Right;
    VolumeBarBackground->Anchor.RightOffset= Height*0.5f;

    VolumeBarBackground->Anchor.VerticalCenterReference=VolumeBar;
    VolumeBarBackground->FillColor=SColor(0.5f,0.5f,0.5f,1.0f);
    VolumeBarBackground->Radius=ProgressBarHeight*0.5f;
    RRectangleWidgetWeakPtr VolumeBarBackgroundWeak=VolumeBarBackground;

    auto VolumeBarProgress = NewWidget<RRectangleWidget>(VolumeBarBackground);
    VolumeBarProgress->Height=ProgressBarHeight;
    VolumeBarProgress->Width=[this,VolumeBarBackgroundWeak]()->float
    {
        return ViewModel->GetVolume()*VolumeBarBackgroundWeak->PresentSize.X;
    };
    VolumeBarProgress->FillColor=SColor::Black;
    VolumeBarProgress->Radius=ProgressBarHeight*0.5f;



    auto VolumeBall = NewWidget<RRectangleWidget>(VolumeBarProgress);
    VolumeBall->Radius=DragBallSize*0.5f;
    VolumeBall->FillColor=SColor::Black;
    VolumeBall->Width=DragBallSize;
    VolumeBall->Height=DragBallSize;
    VolumeBall->Anchor.HorizontalCenterReference=VolumeBarProgress;
    VolumeBall->Anchor.HorizontalCenterReferenceEdge=EHorizontalWidgetEdge::Right;
    VolumeBall->Anchor.VerticalCenterReference=VolumeBar;

    auto VolumeMouse = NewWidget<RMouseWidget>(VolumeBarProgress);
    VolumeMouse->Anchor.HorizontalFill(VolumeBarProgress);
    VolumeMouse->Anchor.VerticalCenterReference=VolumeBarProgress;
    VolumeMouse->Height=DragBallSize;
    VolumeMouse->Drag->bIsEnabled=true;
    RRectangleWidgetWeakPtr VolumeBarProgressWeak=VolumeBarProgress;
    VolumeMouse->Drag->OnDragMoved=[this,VolumeBarBackgroundWeak,VolumeBarProgressWeak](
        const SVec2& )
    {
        float NewX=VolumeBarBackgroundWeak->GetMousePositionInWidget().X;
        float NewPercent=CMath::Clamp(NewX/VolumeBarBackgroundWeak->PresentSize.X,0.0f,1.0f);
        ViewModel->SetVolume(NewPercent);
    };

}
